package text;

import domain.Student;

//利用对象来封装数据
import java.util.ArrayList;
import java.util.Scanner;

public class Studentmannger {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        lo:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            System.out.println("啦啦啦:");
            System.out.println("master新增:");


            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent(list);
                    break;

                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updataStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    break lo;
                default:
                    System.out.println("输入有误");
                    break;

            }
        }
    }




    public static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无数据，请先录入");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日\t");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getBirthday());

        }
    }

    public static void addStudent(ArrayList<Student> list) {
//        添加学生的方法
        Scanner sc = new Scanner(System.in);
        String sid;
        while(true){
//            学号唯一
            System.out.println("请输入学号");
            sid = sc.next();
            int index=getindex(list,sid);
                    if(index==1){
                        break;
                    }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Student stu = new Student(sid, name, age, birthday);
//        将数据封装成对象
        list.add(stu);
//        讲对象存入集合
        System.out.println("添加成功");
    }

    public static int getindex(ArrayList<Student> list, String sid) {
//        遍历得到学号在集合中索引位置  如果没有该学号则返回负一
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String id = stu.getSid();
            if (id.equals(sid)) {
                index = i;
            }

        }
        return index;

    }

    public static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入您要删除的学生学号");
        Scanner sc = new Scanner(System.in);
        String deleteSid =sc.next();
        int index=getindex(list,deleteSid);
        if(index==-1){
            System.out.println("无此学号");
            return;
        }
        else{
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    public static void updataStudent(ArrayList<Student> list) {
        System.out.println("请输入您要修改的学生学号");
        Scanner sc = new Scanner(System.in);
        String updataSid =sc.next();
        int index=getindex(list,updataSid);
        if(index==-1){
            System.out.println("无此学号");
            return;
        }
        else{
            System.out.println("请输入学号");
            String sid = sc.next();
            System.out.println("请输入姓名");
            String name = sc.next();
            System.out.println("请输入年龄");
            int age = sc.nextInt();
            System.out.println("请输入生日");
            String birthday = sc.next();
            Student stu = new Student(sid, name, age, birthday);



            list.set(index,stu);
            System.out.println("修改完成");
        }
    }

}
